import 'package:flutter/material.dart';
import 'package:xws/helpers/safe_state.dart';
import 'package:xws/models/user/user_obj.dart';
import 'package:xws/res/icons.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/refresh_widget/refresh_load_view.dart';
import 'package:xws/widgets/safe_scaffold.dart';

import 'friedn_item.dart';
import 'search_user.dart';

/// 好友管理
class FriendsPage extends StatefulWidget {
  const FriendsPage({Key? key}) : super(key: key);

  @override
  _FriendsPageState createState() => _FriendsPageState();
}

class _FriendsPageState extends State<FriendsPage> with SafeState<FriendsPage> {
  late RefreshViewController<User> _viewController;

  @override
  void initState() {
    super.initState();
    _viewController = RefreshViewController<User>();
  }

  @override
  void dispose() {
    _viewController.dispose();
    super.dispose();
  }

  /// 刷新
  Future<void> _refresh() async {
    await _viewController.refresh();
  }

  @override
  Widget build(BuildContext context) {
    return SafeScaffold(
      titleText: '好友管理',
      actions: <Widget>[
        IconButton(
          onPressed: () async => await push(context, SearchUser(refresh: _refresh)),
          icon: const Icon(IR.search, size: 20),
        ),
      ],
      body: _buildList,
    );
  }

  Widget get _buildList {
    return ColoredBox(
      color: white(context),
      child: RefreshLoadView<User>(
        viewController: _viewController,
        onRefresh: (int v, _) async {
          return await getFriedns(pageNum: 1);
        },
        onLoad: (int page, _) async {
          return await getFriedns(pageNum: page);
        },
        itemBuilder: (int index, User u) => FriendItem(
          user: u,
          refresh: _refresh,
        ),
      ),
    );
  }
}
